// ///////////////////////////// MIT License //////////////////////////////////// //
//                                                                                //
// Copyright (c) 2010 David Zsolt Manrique                                        //
//                    david.zsolt.manrique@gmail.com                              //
//                                                                                //
// Permission is hereby granted, free of charge, to any person obtaining a copy   //
// of this software and associated documentation files (the "Software"), to deal  //
// in the Software without restriction, including without limitation the rights   //
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell      //
// copies of the Software, and to permit persons to whom the Software is          //
// furnished to do so, subject to the following conditions:                       //
//                                                                                //
// The above copyright notice and this permission notice shall be included in     //
// all copies or substantial portions of the Software.                            //
//                                                                                //
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR     //
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,       //
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE    //
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER         //
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,  //
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN      //
// THE SOFTWARE.                                                                  //
//                                                                                //
// ////////////////////////////////////////////////////////////////////////////// //

#include "control_io.h"



void load_xyz(const varname_t & varname, const xyzfilelist_t & filelist,
		std::vector<unitcell_t> & unitcells,
        std::vector<double> & energies,
		std::vector<position_t> & positions,
		label_t & label,
		position_t & min_position,
        double & min_energy)
{
	min_energy = std::numeric_limits<double>::max();
	io::xyz<vector_type> min_frame;
	for(xyzfilelist_t::const_iterator file = filelist.begin(); file != filelist.end(); file++)
    {

		
		

        std::ifstream ifs(file->c_str());
        io::xyz<vector_type> frame;
        while(ifs >> frame) // make sure frame is totally reseted!! TODO
        {
			
            position_t p;
			p.resize(frame.data().size());
            for(std::vector<Eigen::Vector3d>::size_type i = 0; i < p.size(); i++)
                p[i] = frame.data()[i].coord;
            positions.push_back(p);
            
			unitcell_t uc;
			uc.col(0) = frame.a1();
			uc.col(1) = frame.a2();
			uc.col(2) = frame.a3();
			unitcells.push_back(uc);
			
            double e = frame.var_double("energy");
            if (e < min_energy)
            {
                min_energy = e;
                min_frame = frame;
            } 
            energies.push_back(e);
        }
        ifs.close();

    }

	min_position.resize(min_frame.data().size());
	for(std::vector<Eigen::Vector3d>::size_type i = 0; i < min_position.size(); i++)
            min_position[i] = min_frame.data()[i].coord;
	label.resize(min_frame.data().size());
	for(std::vector<Eigen::Vector3d>::size_type i = 0; i < min_position.size(); i++)
            label[i] = min_frame.data()[i].label;

}


